/* uc65 - Micro Code for 6502/NES
 * Copyright (C) 2013  Norman B. Lancaster
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.gmail.qbradq.uc65;

/**
 * An abstraction of a variable definition.
 */
public class Variable {
	private String name;
	private int width;
	
	/**
	 * Construct a new variable definition.
	 * @param name The full name of the variable including module scope.
	 * @param width The width of the variable in bytes.
	 */
	public Variable(String name, int width) {
		this.name = name;
		this.width = width;
	}
	
	/**
	 * Generates variable code for this variable.
	 * @param line The source line the variable was defined on.
	 * @param extern A flag to say if the variable should be exported.
	 * @param ramBank The ram bank to place the variable in.
	 * @param gen The CodeGenerator object to use.
	 */
	public void generateCode(SourceLine line, boolean extern, int ramBank, CodeGenerator gen) {
		gen.emitVariable(line, name, 0, extern, ramBank);
		for(int i = 0; i < width; ++i) {
			gen.emitVariable(line, getNameForByte(i), 1, extern, ramBank);
		}
	}
	
	/**
	 * @return The byte width of the variable.
	 */
	public int getWidth() {
		return width;
	}
	
	/**
	 * @param i The byte number to reference.
	 * @return The assembly label name for this byte of the variable.
	 */
	public String getNameForByte(int i) {
		char c = (char)('0' + i);
		return name + "_" + c;
	}
}
